import 'package:flutter/material.dart';
import '../services/deepseek_service.dart';

class AITreatmentResultPage extends StatefulWidget {
  final String diagnosis;
  final String treatmentGoals;
  final String medicalHistory;
  final String medications;
  final String allergies;

  const AITreatmentResultPage({
    Key? key,
    required this.diagnosis,
    required this.treatmentGoals,
    required this.medicalHistory,
    required this.medications,
    required this.allergies,
  }) : super(key: key);

  @override
  State<AITreatmentResultPage> createState() => _AITreatmentResultPageState();
}

class _AITreatmentResultPageState extends State<AITreatmentResultPage> {
  final List<String> _treatmentResults = [];
  bool _isGenerating = true;
  final DeepSeekService _deepSeekService =
      DeepSeekService('sk-4e0e104af45b432d86f0b95db6681b9f');

  @override
  void initState() {
    super.initState();
    _generateTreatmentPlan();
  }

  Future<void> _generateTreatmentPlan() async {
    try {
      final prompt = '''
请根据以下患者信息制定专业的治疗方案：

诊断结果：${widget.diagnosis}
治疗目标：${widget.treatmentGoals}
既往病史：${widget.medicalHistory}
当前用药：${widget.medications}
过敏史：${widget.allergies}

请提供以下方面的治疗方案：
1. 药物治疗方案
2. 非药物治疗建议
3. 生活方式调整建议
4. 随访计划
5. 注意事项

请以专业医生的角度制定方案，并给出详细的建议，请并不要使用markdown语法输出
''';

      final response = await _deepSeekService.getMedicalAdvice(prompt);

      if (mounted) {
        setState(() {
          _treatmentResults.addAll(response.split('\n'));
          _isGenerating = false;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _treatmentResults.add('生成治疗方案时出错：$e');
          _isGenerating = false;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Stack(
          children: [
            Column(
              children: [
                // 顶部绿色背景区域
                Container(
                  padding: const EdgeInsets.only(
                      left: 20, right: 20, bottom: 20, top: 10),
                  color: const Color(0xFF1DC4A0),
                  child: Row(
                    children: [
                      IconButton(
                        icon: const Icon(Icons.arrow_back, color: Colors.white),
                        onPressed: () => Navigator.pop(context),
                        padding: EdgeInsets.zero,
                      ),
                      const SizedBox(width: 10),
                      const Text(
                        'AI治疗方案',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 24,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                ),
                // 治疗方案区域
                Expanded(
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.all(20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        if (_isGenerating)
                          const Center(
                            child: CircularProgressIndicator(),
                          )
                        else
                          ..._treatmentResults.map((result) => Padding(
                                padding: const EdgeInsets.only(bottom: 10),
                                child: Text(
                                  result,
                                  style: const TextStyle(fontSize: 16),
                                ),
                              )),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
